﻿using System.IO;
using System.Text;
using System;
using System.Linq;

namespace ProtocolTool
{
    partial class Program
    {
        //ClassTypeScript
        public static readonly string Filepath_ClassJavaScript = @"proto.js";

        public static readonly string Template_ClassJavaScript =
            @"/* eslint-disable comma-dangle */
/* eslint-disable no-unused-vars */
var MapProtocolId = {
$Class31$
  properties: {
$Class32$
  }
}

$Class2$
export default {

$Class4$
  MapProtocolId: MapProtocolId

}
";

        /// <summary>
        /// 生成结构体文件 JavaScript
        /// </summary>
        private static void ProtocolConverterClassJavaScript()
        {
            Show("ProtocolConverterClassJavaScript");
            var sb2 = new StringBuilder();
            var sb31 = new StringBuilder();
            var sb32 = new StringBuilder();
            var sb4 = new StringBuilder();

            foreach (var kvp in DictClass)
            {
                if (kvp.Value.ClassType == 0)
                {
                    sb31.AppendLine($"  {kvp.Value.NameId}: {kvp.Value.Id}, // {kvp.Value.Desc}");

                    sb32.AppendLine($"    {kvp.Value.Id}: {{ name: \'{kvp.Value.NameId}\', value: {kvp.Value.Id}, desc: \'{kvp.Value.Desc}\' }},");
                }
            }

            foreach (var kvp in DictEnum)
            {
                if (kvp.Value.Desc != "")
                {
                    sb2.AppendLine($"// {kvp.Value.Desc}");
                }
                sb2.AppendLine($"var Map{kvp.Value.Name} = {{");
                foreach (var kvp2 in kvp.Value.DictBody)
                {
                    sb2.Append($"  {kvp2.Value.Body}");
                    if (kvp2.Value.Value != -100000)
                    {
                        sb2.Append($": {kvp2.Value.Value},");
                    }
                    if (kvp2.Value.Desc != "")
                    {
                        sb2.Append($" // {kvp2.Value.Desc}");
                    }
                    sb2.Append("\r\n");
                }
                sb2.Append("}\r\n");

                sb4.AppendLine($"  Map{kvp.Value.Name}: Map{kvp.Value.Name},");
            }


            string txt = Template_ClassJavaScript;
            FileStream fs = new FileStream(PathCurrent + Filepath_ClassJavaScript, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            txt = txt.Replace("$Class2$", sb2.ToString());
            txt = txt.Replace("$Class31$", sb31.ToString());
            txt = txt.Replace("$Class32$", sb32.ToString());
            txt = txt.Replace("$Class4$", sb4.ToString());
            sw.Write(txt);
            sw.Close();
            Show("输出文件：" + Filepath_ClassJavaScript);
        }

    }
}